package com.example.cpoopc.helloretrofit;

import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;

import com.example.cpoopc.helloretrofit.api.ForumService;
import com.example.cpoopc.helloretrofit.bean.PostListEntity;
import com.example.cpoopc.helloretrofit.bean.PostListRequestBody;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.net.URI;

import retrofit.Callback;
import retrofit.Profiler;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.converter.ConversionException;
import retrofit.converter.Converter;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;


public class MainActivity extends ActionBarActivity {
    String tag = "cp:retrofit";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doRequest();
            }
        });
    }

    private void doRequest() {
        RequestInterceptor requestInterceptor = new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader(Uri.encode("zidingyidoubu"),Uri.encode("hahhahahahhaa"));
            }
        };
        Profiler profiler = new Profiler() {
            @Override
            public Object beforeCall() {

                return null;
            }

            @Override
            public void afterCall(RequestInformation requestInfo, long elapsedTime, int statusCode, Object beforeCallData) {

            }
        };
//        Converter converter = new Converter() {
//            @Override
//            public Object fromBody(TypedInput body, Type type) throws ConversionException {
//                return null;
//            }
//
//            @Override
//            public TypedOutput toBody(Object object) {
//                return null;
//            }
//        };
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("http://mtest1.starapp.99.com:6869")
                .setRequestInterceptor(requestInterceptor)
                .setProfiler(profiler)
//                .setConverter(converter)
                .build();
        ForumService forumService = restAdapter.create(ForumService.class);
        Callback<PostListEntity> callback = new Callback<PostListEntity>() {
            @Override
            public void success(PostListEntity postListEntity, Response response) {
                Log.e(tag,"success: " + response.getReason());
                Log.e(tag,"success: postListEntity" + postListEntity);

            }

            @Override
            public void failure(RetrofitError error) {
                Log.e(tag,"failure:"+error.getMessage());
            }
        };
//        forumService.getPostList("new",null,"0","0","20",null,null,callback);
//        JSONObject jsonObject = new JSONObject();
//        try {
//            jsonObject.put("type","new");
//            jsonObject.put("min_ts",0);
//            jsonObject.put("page_size",20);
//            jsonObject.put("max_ts",0);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        forumService.getPostList(jsonObject.toString(),callback);

        PostListRequestBody body = new PostListRequestBody("new", 0, 0, 20);
        forumService.getPostList(body,callback);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
